DZApp.service('orderUnitService', function(commonService) {
	return {
		showList: function() {
			var url = "/operationService/listOrderUnit"; 
			return commonService.commonAjaxByPost(url);
		},
		editData: function(postData){
			var url = "/operationService/saveOrUpdateOrderUnit";
			return commonService.commonAjaxPostByText(url, postData);
		},
		delData: function(postData){
			var url = "/operationService/deleteOrderUnit";
			return commonService.commonAjaxByPost(url, postData);
		}
	};
});

DZApp.controller('orderUnitController', function(
		$scope, orderUnitService, $state) {
	orderUnitService.showList().then(function(data) {
		$scope.showList = eval(data.bizData);
	});
	$scope.theObj = null;
	$scope.editData = function() {
		if (!bValidateObj.bValidateCall($('#editData'))) {
			return false;
		}
		var postData = $scope.edit;
		orderUnitService.editData(postData).then(function(data) {
			if (data.retCode == 'success') {
				if ($scope.openTitle == '新增') {
					$state.reload();
				} else {
					$('#editData').modal('hide');
					for ( var p in $scope.theObj) {
						$scope.theObj[p] = $scope.edit[p];
					}
					$state.reload();
				}
			} else {
				bootboxWindow.alert('保存失败，' + data.errorMsg, null);
			}
		});
	};
	$scope.addInit = function() {
		$scope.openTitle = '新增';
		$scope.edit = {
			unitNo : null,
			name : '',
			description : '',
			isValid : 1
		};
	};
	$scope.setInit = function(_obj) {
		$scope.theObj = _obj;
		$scope.openTitle = '修改';
		$scope.edit = angular.copy(_obj);
	};
	$scope.delInit = function(_obj) {
		if(_obj.isValid == 1){
			bootboxWindow.alert('请先将计量单位置为无效才能删除！', null);
		}else{
			bootboxWindow.confirm('确定要删除计量单位？', function(){
				var postData = $.param({
					unitNo : _obj.unitNo
				});
				orderUnitService.delData(postData).then(function(data) {
					if (data.retCode == 'success') {
						$state.reload();
					} else {
						bootboxWindow.alert('删除失败，请重试', null);
					}
				});
			}, null);
		}
	};
});